package cn.jit.ssm.web.controller.demo;

import cn.jit.ssm.support.JSONReturn;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 文件上传示例
 *
 * @author: Cecurio
 * @create: 2017-12-17 16:43
 **/
@Controller
@RequestMapping(value = "/upload")
public class FileUploadController {

    /**
     * 多个文件上传
     *
     * @param files
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/multiFile", method = RequestMethod.POST)
    @ResponseBody
    public JSONReturn multiFileUpload(@RequestParam("files") MultipartFile[] files) throws IOException {

        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            System.out.println("文件内容为: " + IOUtils.toString(file.getInputStream(), "UTF-8"));
            if (file.isEmpty()) {
                return JSONReturn.buildFailure("uploadFailure");
            }
        }

        return JSONReturn.buildSuccess("uploadSuccess");
    }

    /**
     * 单个文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/singleFile", method = RequestMethod.POST)
    @ResponseBody
    public JSONReturn singleFileUpload(@RequestParam("file") MultipartFile file) throws IOException {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
            System.out.println("文件内容为: " + IOUtils.toString(file.getInputStream(), "UTF-8"));
            return JSONReturn.buildSuccess("uploadSuccess");
        }

        return JSONReturn.buildFailure("uploadFailure");
    }

    /**
     * 上传文件、提交表单一块进行
     *
     * @param name
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/singleFileWithName", method = RequestMethod.POST)
    @ResponseBody
    public JSONReturn singleFileUploadWithParam(@RequestParam("name") String name,
                                                @RequestParam("file") MultipartFile file,
                                                HttpServletRequest request) throws IOException {
        System.out.println("name=" + name);
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
            ServletContext sc = request.getSession().getServletContext();
            String dir = sc.getRealPath("/upload");    //设定文件保存的目录
            System.out.println("dir=" + dir);
            File uploadDir = new File(dir);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }

            String filename = file.getOriginalFilename();    //得到上传时的文件名

            // 重命名文件
            long _lTime = System.nanoTime();
            String _ext = filename.substring(filename.lastIndexOf("."));
            filename = _lTime + _ext;

            // 文件的复制
            FileUtils.writeByteArrayToFile(new File(uploadDir, filename), file.getBytes());
            System.out.println("文件内容为: " + IOUtils.toString(file.getInputStream(), "UTF-8"));
            return JSONReturn.buildSuccess("uploadSuccess");
        }

        return JSONReturn.buildFailure("uploadFailure");
    }
}
